/**
 * Created by luo yu shi @ zhongshu on 2018/12/20.
 * @flow
 */

'use strict';


var fs = require('fs-extra');
const path = require('path');
var childProcess = require('child_process')
var exec = childProcess.exec;


/**
 * exec process stdio
 */

var spawn = require('child_process').spawn;
var children  = [];
function shspawn(command) {
    children.push(spawn('sh', ['-c', command], { stdio: 'inherit' }));
}


const packageJson = require('../package.json');

const moduleName = packageJson.name;
const outFileZip = 'outFileZip';


let exec_file = '';
exec_file += `rm -rf ${outFileZip}`;
exec_file += '&&';
exec_file += `mkdir ${outFileZip}`;
exec_file += `&&`;
exec_file += bundleFile('ios');
exec_file += '&&';
exec_file += bundleFile('android');
bundleFile('android');
exec_file += '&&';
exec_file += `npm run prod && mkdir ${outFileZip}/test && cp -r web/src_dist/ outFileZip/test && cd outFileZip && zip -r test.zip test/`;
shspawn(exec_file);
shspawn('echo ========== 模块zip文件s生成成功，请查看 outFileZip文件夹 =============');


function bundleFile(platform) {
    const distPath = `${outFileZip}/${platform}.so`;

    //./make-fullpack-jsbundles.sh -p ios -m finance_chi_cang
    let exec_file =  '';
    exec_file += `cd scripts/`;
    exec_file += `&& ./make-fullpack-jsbundles.sh -p ${platform} -m ${moduleName}`;
    exec_file += `&& cd ../`;
    exec_file += `&& pwd`;
    exec_file += `&& cp ./outputBundle/${moduleName}.so ${distPath}`;

    return (exec_file);
}